<template>
	<uni-popup ref="popup" type="bottom" @change="popChange">
		<view class="sx-address-picker">
			<view class="sx-address-picker__close">
				<uni-icons class="absolute" size="20" color="#ccc" type="closeempty"
					@click.native="$refs.popup.close"></uni-icons>
			</view>
			<view class="sx-address-picker__title">
				请选择所在地区
			</view>
			<!-- 已选择区域 -->
			<view class="list">
				<view v-if="province" class="item" @click="activeLevel = 0">
					<view class="left">{{province.name}}</view>
					<view class="right">
						<img class="arrow" src="/static/icons/arrow-right.png" alt="">
					</view>
				</view>
				<view v-if="city" class="item" @click="activeLevel = 1">
					<view class="left">{{city.name}}</view>
					<view class="right">
						<img class="arrow" src="/static/icons/arrow-right.png" alt="">
					</view>
				</view>
				<view v-if="area" class="item" @click="activeLevel = 2">
					<view class="left">{{area.name}}</view>
					<view class="right">
						<img class="arrow" src="/static/icons/arrow-right.png" alt="">
					</view>
				</view>
			</view>
			<!-- <uni-list :border="false">
				<uni-list-item v-if="province" show-arrow clickable @click="activeLevel = 0">
					<template v-slot:header>
						<view class="sx-address-picker__select--active">
							{{province.name}}
						</view>
					</template>
				</uni-list-item>
				<uni-list-item v-if="city" show-arrow clickable @click="activeLevel = 1">
					<template v-slot:header>
						<view class="sx-address-picker__select--active">
							{{city.name}}
						</view>
					</template>
				</uni-list-item>
				<uni-list-item v-if="area" show-arrow clickable @click="activeLevel = 2">
					<template v-slot:header>
						<view class="sx-address-picker__select--active">
							{{area.name}}
						</view>
					</template>
				</uni-list-item>
			</uni-list> -->
			<view class="sx-address-picker--divider"></view>
			<view class="sx-address-picker__list">
				<view class="sx-address-picker__list-title">
					{{selectTitle}}
				</view>
				<view class="sx-address-picker__list-content" @click="reGetList">
					<uni-load-more v-if="loadingStatus !== 'more'" :status="loadingStatus"
						:content-text="{contentnomore:'加载失败，请点击重试'}"></uni-load-more>
					<!-- 省选择 -->
					<view class="list" v-if="activeLevel == 0">
						<view class="item" @click="selectProvince(item)" v-for="(item, index) in provinceList"
							:key="index">
							<view class="left">{{item.name}}</view>
							<view class="right">
								<uni-icons v-if="province && item.provinceId === province.provinceId"
									type="checkmarkempty" class="sx-address-picker__list-tab--active">
								</uni-icons>
							</view>
						</view>
					</view>
					<!-- 市选择 -->
					<view class="list" v-if="activeLevel == 1">
						<view class="item" @click="selectCity(item)" v-for="(item, index) in cityList" :key="index">
							<view class="left">{{item.name}}</view>
							<view class="right">
								<uni-icons v-if="city && item.cityId === city.cityId" type="checkmarkempty"
									class="sx-address-picker__list-tab--active"></uni-icons>
							</view>
						</view>
					</view>
					<!-- 区选择 -->
					<view class="list" v-if="activeLevel == 2">
						<view class="item" @click="selectArea(item)" v-for="(item, index) in areaList" :key="index">
							<view class="left">{{item.name}}</view>
							<view class="right">
								<uni-icons v-if="area && item.countyId === area.countyId" type="checkmarkempty"
									class="sx-address-picker__list-tab--active"></uni-icons>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</uni-popup>
</template>
<script>
	import uniTransition from '../uni-transition/uni-transition.vue';
	import uniIcons from '../uni-icons/uni-icons.vue';
	export default {
		props: {
			value: Boolean,
			selected: {
				type: Array,
				default () {
					return []
				}
			},
		},
		data() {
			return {
				world: '0',
				loadingStatus: 'loading',
				list: [], // 省列表
				activeLevel: 0,

				provinceList: [],
				cityList: [],
				areaList: [],
				province: null,
				city: null,
				area: null,
			}
		},
		methods: {
			// 弹窗是否显示状态事件
			popChange(e) {
				const status = e.show;
				// 选择省市区未选择完毕重置
				if (this.activeLevel !== 2 && !status) {
					this.activeLevel = 0;
				}
				this.$emit('input', status);
			},

			// 省
			selectProvince(item, index) {
				this.activeLevel = 1;
				this.province = item;
				this.getCity();
			},
			selectCity(item) {
				this.activeLevel = 2;
				this.city = item;
				this.getArea();
			},
			selectArea(item) {
				// this.activeLevel = 2;
				this.area = item
				this.$refs.popup.close();
				this.$emit('confirm', [this.province, this.city, this.area]);
			},
			// 获取所有省份
			getAllProvince() {
				this.$httpRequest.request({
					url: 'wechat/getAllProvince',
					method: 'get',
					success: res => {
						if (res.code === this.$httpConfig.SUCCESS_CODE) {
							this.provinceList = res.data;
							this.loadingStatus = 'more';
						}
					}
				});
			},

			// 获取所有城市
			getCity() {
				this.$httpRequest.request({
					url: 'wechat/getAllCity',
					method: 'get',
					params: {
						provinceId: this.province ? this.province.provinceId : undefined
					},
					success: res => {
						if (res.code === this.$httpConfig.SUCCESS_CODE) {
							this.cityList = res.data;
							this.loadingStatus = 'more';
						}
					}
				});
			},

			// 获取所有区
			getArea() {
				this.$httpRequest.request({
					url: 'wechat/getAllCounty',
					method: 'get',
					params: {
						cityId: this.city ? this.city.cityId : undefined
					},
					success: res => {
						if (res.code === this.$httpConfig.SUCCESS_CODE) {
							this.areaList = res.data;
							this.loadingStatus = 'more';
						}
					}
				});
			},

			// 获取列表
			reGetList() {
				if (this.loadingStatus === 'noMore') {
					this.getList();
				}
			},

			getList() {
				if (this.activeLevel === 0) {
					this.getAllProvince();
				} else if (this.activeLevel === 1) {
					this.getCity();
				} else if (this.activeLevel === 2) {
					this.getArea();
				}
			}
		},
		computed: {
			// 动态标题显示
			selectTitle() {
				const titles = {
					0: '请选择省份/地区',
					1: '请选择城市',
					2: '请选择区/县',
				};
				return titles[this.activeLevel];
			}
		},
		watch: {
			// 监听 `value` 触发弹窗显示状态以及事件处理
			value(val) {
				if (val) {
					this.$refs.popup.open();
					// const countyName = this.selected[0];
					// this.world = this.selected.length === 1 ? '1' : '0';
					// if (this.activeItems.length === 4) {
					// 	this.activeLevel = 2;
					// }
					this.getList();
				} else {
					this.$refs.popup.close();
				}
			},
			province(val) {
				this.city = null;
				this.area = null;
			},
			city(val) {
				this.area = null;
			}
		}
	};
</script>
<style lang="scss" scoped>
	.sx-address-picker {
		border-top-left-radius: 20px;
		border-top-right-radius: 20px;
		background-color: white;
		padding-left: 8px;
		padding-right: 8px;
		padding-top: 16px;
		font-size: 16px;

		&__close {
			text-align: right;
			margin-right: 16px;
			margin-top: 4px;
			position: absolute;
			left: 0;
			right: 0;
		}

		&__title {
			text-align: center;
			font-weight: 500;
			font-size: 18px;
			margin-bottom: 20px;
		}

		&__select {
			&--active {
				color: $uni-color-primary;
			}
		}

		&--divider {
			border-top: 0.5px solid #ddd;
		}

		&__list {
			max-height: 400px;
			overflow-y: auto;
			margin-top: 12px;

			&-tab {
				display: flex;
				margin-bottom: 20px;
				justify-content: space-between;
				padding-left: 16px;
				padding-right: 16px;

				&--hide {
					display: none;
				}

				&--active {
					color: $uni-color-primary !important;
				}
			}

			&-title {
				font-weight: bold;
				margin-bottom: 16px;
			}

			.uni-load-more {
				padding: 100px 0;
			}
		}
	}

	.list {
		.item {
			display: flex;
			padding: 20rpx 30rpx;
			flex: 1;
			color: #3b4144;
			justify-content: space-between;
			overflow: hidden;
			font-size: 28rpx;
			position: relative;

		}

		.arrow {
			width: 28rpx;
			height: 30rpx;
		}
	}
</style>